package com.example.mybatisdemo.service.impl;

import com.example.mybatisdemo.entity.bo.UserInfo;
import com.example.mybatisdemo.entity.dto.UserInfoDto;
import com.example.mybatisdemo.entity.vo.UserInfoVo;
import com.example.mybatisdemo.entity.vo.UserLoginInfoVo;
import com.example.mybatisdemo.mapper.UserInfoMapper;
import com.example.mybatisdemo.service.UserInfoService;
import com.example.mybatisdemo.util.IdUtils;
import com.example.mybatisdemo.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Resource
    private RedisUtils redisUtils;

    @Override
    public UserInfoDto save(UserInfoVo userInfoVo) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(IdUtils.getSnowflakeId());
        userInfo.setName(userInfoVo.getName());
        userInfo.setMobile(userInfoVo.getMobile());
        userInfo.setPass(userInfoVo.getPass());
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        userInfoMapper.insertUser(userInfo);

        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUserId(userInfo.getId());
        return userInfoDto;
    }

    @Override
    public void update(Long id, UserInfoVo userInfoVo) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setName(userInfoVo.getName());
        userInfo.setMobile(userInfoVo.getMobile());
        userInfo.setPass(userInfoVo.getPass());
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        userInfoMapper.updateByPrimaryKey(userInfo);
    }

    @Override
    public Boolean del(Long id) {
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
        if (userInfo.getIsDeleted() == 1) {
            return false;
        } else {
            userInfoMapper.deleteByPrimaryKey(userInfo);
            return true;
        }
    }

    @Override
    public UserInfoDto get(Long id) {
        UserInfo userInfo= userInfoMapper.selectByPrimaryKey(id);
        if (userInfo.getIsDeleted() == 1) {
            return null;
        }
        return convertToDto(userInfo);
    }

    @Override
    public List<UserInfoDto> getAll() {
        List<UserInfoDto> list = new ArrayList<>();
        for(UserInfo userInfo : userInfoMapper.selectAll()) {
            if (userInfo.getIsDeleted() == 1) {
                continue;
            }
            list.add(convertToDto(userInfo));
        }
        return list;
    }

    public UserInfoDto convertToDto(UserInfo userInfo) {
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUserId(userInfo.getId());
        userInfoDto.setName(userInfo.getName());
        userInfoDto.setMobile(userInfo.getMobile());
        userInfoDto.setCreateTime(new Date());
        userInfoDto.setUpdateTime(new Date());
        return userInfoDto;
    }

    @Override
    public Boolean existMobile(String mobile) {
        if (userInfoMapper.selectPassByMobile(mobile) == null) {
            return false;
        }
        return true;
    }

    @Override
    public String findPassByMobile(String mobile) {
        return userInfoMapper.selectPassByMobile(mobile);
    }

    @Override
    public String writeInRedis(UserLoginInfoVo userLoginInfoVo) {
        Long id = userInfoMapper.selectIdByMobile(userLoginInfoVo.getMobile());
        UUID uuid = UUID.randomUUID();
        redisUtils.set(uuid.toString(), id.toString());
        return uuid.toString();
    }

    @Override
    public void logout(String token) {
        redisUtils.delete(token);
    }
}
